import os

import requests
from jenkins import Jenkins

jenkins_url = "http://localhost:8080/" #打开jenkins的网址：启动jenkins的主机ip名+端口号
server = Jenkins(jenkins_url, username='admin', password='123456')

job_name = "job/apitest_meike"
#job_url = jenkins_url + job_name,这条代码也行，但下条更好
job_url = server.get_info(job_name)['url']
job_last_number = server.get_info(job_name)['lastBuild']['number']
report_url = job_url + str(job_last_number) + '/allure'

def push_message():
    content = {}
    #os.getcwd()='D:\wode\python\lianxi-project\requests\message'
    file_path = os.path.dirname(os.getcwd()) + '\\allure-report\export\prometheusData.txt'
    f = open(file_path)
    for line in f.readlines():
        launch_name = line.strip('\n').split(' ')[0]#str.strip('\n'),去掉换行符，再通过空格分割str
        num = line.strip('\n').split(' ')[1]
        content.update({launch_name: num})
    f.close()
    print(content)
    passed_num = content['launch_status_passed']#通过数量
    failed_num = content['launch_status_failed']#失败数量
    broken_num = content['launch_status_broken']#阻塞数量，指还没运行到断言就已经报错
    skipped_num = content['launch_status_skipped']#跳过数量
    case_num = content['launch_retries_run']#总数量

    """钉钉消息发送，通过webhook发送消息"""
    webhook = ""
    content = {
        "msqtype":"text",
        "text":{
            "content":"接口自动化脚本执行结果：\n运行总数" + case_num
            + "\n通过数量：" + passed_num
            + "\n失败数量：" + failed_num
            + "\n阻塞数量：" + broken_num
            + "\n跳过数量：" + skipped_num
            + "\n构建地址：\n" + job_url
            + "\n报告地址：" + report_url

        }
    }
    requests.post(url=webhook,json=content,verify=False)#因为它是https的,加上verify=False可以不报警报
